<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>newlines.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_string_newlines.js.html">newlines.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for string newlines.
<a name="line17"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>/**
<a name="line22"></a> * Namespace for string utilities
<a name="line23"></a> */
<a name="line24"></a>goog.provide(&#39;goog.string.newlines&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.array&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Splits a string into lines, properly handling universal newlines.
<a name="line31"></a> * @param {string} str String to split.
<a name="line32"></a> * @param {boolean=} opt_keepNewlines Whether to keep the newlines in the
<a name="line33"></a> *     resulting strings. Defaults to false.
<a name="line34"></a> * @return {!Array.&lt;string&gt;} String split into lines.
<a name="line35"></a> */
<a name="line36"></a>goog.string.newlines.splitLines = function(str, opt_keepNewlines) {
<a name="line37"></a>  var lines = goog.string.newlines.getLines(str);
<a name="line38"></a>  return goog.array.map(lines, function(line) {
<a name="line39"></a>    return opt_keepNewlines ? line.getFullLine() : line.getContent();
<a name="line40"></a>  });
<a name="line41"></a>};
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Line metadata class that records the start/end indicies of lines
<a name="line47"></a> * in a string.  Can be used to implement common newline use cases such as
<a name="line48"></a> * splitLines() or determining line/column of an index in a string.
<a name="line49"></a> * Also implements methods to get line contents.
<a name="line50"></a> *
<a name="line51"></a> * Indexes are expressed as string indicies into string.substring(), inclusive
<a name="line52"></a> * at the start, exclusive at the end.
<a name="line53"></a> *
<a name="line54"></a> * Create an array of these with goog.string.newlines.getLines().
<a name="line55"></a> * @param {string} string The original string.
<a name="line56"></a> * @param {number} startLineIndex The index of the start of the line.
<a name="line57"></a> * @param {number} endContentIndex The index of the end of the line, excluding
<a name="line58"></a> *     newlines.
<a name="line59"></a> * @param {number} endLineIndex The index of the end of the line, index
<a name="line60"></a> *     newlines.
<a name="line61"></a> * @constructor
<a name="line62"></a> * @struct
<a name="line63"></a> */
<a name="line64"></a>goog.string.newlines.Line = function(string, startLineIndex,
<a name="line65"></a>                                     endContentIndex, endLineIndex) {
<a name="line66"></a>  /**
<a name="line67"></a>   * The original string.
<a name="line68"></a>   * @type {string}
<a name="line69"></a>   */
<a name="line70"></a>  this.string = string;
<a name="line71"></a>
<a name="line72"></a>  /**
<a name="line73"></a>   * Index of the start of the line.
<a name="line74"></a>   * @type {number}
<a name="line75"></a>   */
<a name="line76"></a>  this.startLineIndex = startLineIndex;
<a name="line77"></a>
<a name="line78"></a>  /**
<a name="line79"></a>   * Index of the end of the line, excluding any newline characters.
<a name="line80"></a>   * Index is the first character after the line, suitable for
<a name="line81"></a>   * String.substring().
<a name="line82"></a>   * @type {number}
<a name="line83"></a>   */
<a name="line84"></a>  this.endContentIndex = endContentIndex;
<a name="line85"></a>
<a name="line86"></a>  /**
<a name="line87"></a>   * Index of the end of the line, excluding any newline characters.
<a name="line88"></a>   * Index is the first character after the line, suitable for
<a name="line89"></a>   * String.substring().
<a name="line90"></a>   * @type {number}
<a name="line91"></a>   */
<a name="line92"></a>
<a name="line93"></a>  this.endLineIndex = endLineIndex;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * @return {string} The content of the line, excluding any newline characters.
<a name="line99"></a> */
<a name="line100"></a>goog.string.newlines.Line.prototype.getContent = function() {
<a name="line101"></a>  return this.string.substring(this.startLineIndex, this.endContentIndex);
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * @return {string} The full line, including any newline characters.
<a name="line107"></a> */
<a name="line108"></a>goog.string.newlines.Line.prototype.getFullLine = function() {
<a name="line109"></a>  return this.string.substring(this.startLineIndex, this.endLineIndex);
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * @return {string} The newline characters, if any (&#39;\n&#39;, \r&#39;, &#39;\r\n&#39;, &#39;&#39;, etc).
<a name="line115"></a> */
<a name="line116"></a>goog.string.newlines.Line.prototype.getNewline = function() {
<a name="line117"></a>  return this.string.substring(this.endContentIndex, this.endLineIndex);
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Splits a string into an array of line metadata.
<a name="line123"></a> * @param {string} str String to split.
<a name="line124"></a> * @return {!Array.&lt;!goog.string.newlines.Line&gt;} Array of line metadata.
<a name="line125"></a> */
<a name="line126"></a>goog.string.newlines.getLines = function(str) {
<a name="line127"></a>  // We use the constructor because literals are evaluated only once in
<a name="line128"></a>  // &lt; ES 3.1.
<a name="line129"></a>  // See http://www.mail-archive.com/es-discuss@mozilla.org/msg01796.html
<a name="line130"></a>  var re = RegExp(&#39;\r\n|\r|\n&#39;, &#39;g&#39;);
<a name="line131"></a>  var sliceIndex = 0;
<a name="line132"></a>  var result;
<a name="line133"></a>  var lines = [];
<a name="line134"></a>
<a name="line135"></a>  while (result = re.exec(str)) {
<a name="line136"></a>    var line = new goog.string.newlines.Line(
<a name="line137"></a>        str, sliceIndex, result.index, result.index + result[0].length);
<a name="line138"></a>    lines.push(line);
<a name="line139"></a>
<a name="line140"></a>    // remember where to start the slice from
<a name="line141"></a>    sliceIndex = re.lastIndex;
<a name="line142"></a>  }
<a name="line143"></a>
<a name="line144"></a>  // If the string does not end with a newline, add the last line.
<a name="line145"></a>  if (sliceIndex &lt; str.length) {
<a name="line146"></a>    var line = new goog.string.newlines.Line(
<a name="line147"></a>        str, sliceIndex, str.length, str.length);
<a name="line148"></a>    lines.push(line);
<a name="line149"></a>  }
<a name="line150"></a>
<a name="line151"></a>  return lines;
<a name="line152"></a>};
</pre>


</body>
</html>
